/**
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-11-24 23:34:52
 * Copyright (c) 2015- joysuch-版权所有
 */

package com.joysuch.wwyt.bp.service.impl;

import com.joysuch.wwyt.bp.entity.BpWorkType;
import com.joysuch.wwyt.bp.repository.BpWorkTypeDao;
import com.joysuch.wwyt.bp.service.BpWorkTypeService;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.core.entity.dsl.QBaseUser;
import com.joysuch.wwyt.core.repository.BaseUserDao;
import com.joysuch.wwyt.core.service.BizCodeGeneratorFacade;
import com.joysuch.wwyt.core.support.Context;
import com.querydsl.core.types.Predicate;
import org.apache.shiro.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class BpWorkTypeServiceImpl implements BpWorkTypeService {

    @Autowired
    private BpWorkTypeDao dao;
    @Autowired
    private BaseUserDao baseUserDao;

    @Autowired
    private BizCodeGeneratorFacade bizCodeGeneratorFacade;

    @Override
    public void add(BpWorkType entity) throws Exception {
        int count = dao.countByName(entity.getName());
        if (count > 0) {
            throw new IllegalArgumentException("此名称已存在");
        }
        if (entity.getStaffCount() == null) {
            entity.setStaffCount(0);
        }
        dao.save(entity);
    }

    @Override
    public void update(BpWorkType entity) throws Exception {
        String name = entity.getName();
        BpWorkType content = dao.findById(entity.getId()).get();
        if (!name.equals(content.getName())) {
            int count = dao.countByName(entity.getName());
            if (count > 0) {
                throw new IllegalArgumentException("此名称已存在");
            }
        }
        content.setName(name);
        content.setDescription(entity.getDescription());
        content.setUpdateBy(Context.getCurrentUserId());
//        content.setUpdateTime(LocalDateTime.now());
        content.setUpdateTime(new Date());
        dao.save(content);
    }

    @Override
    public Page<BpWorkType> findByPage(Pageable page) {
        page = PageDataUtils.addOrderByDesc(page, "id");
        Page<BpWorkType> data = dao.findAll(page);
        for (BpWorkType t : data) {
            Predicate condition = QBaseUser.baseUser.workTypeId.eq(t.getId());
            t.setStaffCount((int) baseUserDao.count(condition));
        }
        return data;
    }

    @Override
    public void delete(Long id) {
        Predicate condition = QBaseUser.baseUser.workTypeId.eq(id);
        Assert.isTrue(baseUserDao.count(condition) == 0, "绑定人员的工种不能删除");
        dao.deleteById(id);
    }

    @Override
    public BpWorkType findById(Long id) {
        return dao.findById(id).get();
    }

    @Override
    public List<BpWorkType> findAllByTenentId(Long tenentId) {
        return dao.findAllByTenentId(tenentId);
    }

}
